package app.base.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import cn.power.shine.R;

public class PntWidget extends RelativeLayout {
	View left;
	View top;
	View right;
	View bottom;
	View horizontalCenter;
	int areapos;
	public final int posL = 0;
	public final int posR = 1;
	public final int posT = 2;
	public final int posB = 3;

	public PntWidget(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.ui_pnt_single, this);
		left = findViewById(R.id.left);
		top = findViewById(R.id.top);
		right = findViewById(R.id.right);
		bottom = findViewById(R.id.down);
		float leftW = 1;
		float rightW = 1;
		float topW = 1;
		float bottomW = 1;
		horizontalCenter = findViewById(R.id.horizontalCenter);
		String weight = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/"+NS.nameSpace,
				"weight");
		String[] weights = weight.split(",");
		if (weights.length == 4) {
			leftW = Float.parseFloat(weights[0]);
			topW = Float.parseFloat(weights[1]);
			rightW = Float.parseFloat(weights[2]);
			bottomW = Float.parseFloat(weights[3]);
		}
		leftW = attrs.getAttributeFloatValue(
				"http://schemas.android.com/apk/res/com.benlai.shopping",
				"leftW", leftW);
		rightW = attrs.getAttributeFloatValue(
				"http://schemas.android.com/apk/res/com.benlai.shopping",
				"rightW", rightW);
		topW = attrs.getAttributeFloatValue(
				"http://schemas.android.com/apk/res/com.benlai.shopping",
				"topW", topW);
		bottomW = attrs.getAttributeFloatValue(
				"http://schemas.android.com/apk/res/com.benlai.shopping",
				"bottomW", bottomW);
		String widget = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/"+NS.nameSpace,
				"type");

		left.setLayoutParams(getLayoutParam(posL, leftW));
		right.setLayoutParams(getLayoutParam(posR, rightW));
		top.setLayoutParams(getLayoutParam(posT, topW));
		bottom.setLayoutParams(getLayoutParam(posB, bottomW));

		horizontalCenter.setLayoutParams(getLayoutParam(posL, 10 - leftW
				- rightW));
		((ViewGroup) findViewById(R.id.content))
				.setLayoutParams(getLayoutParam(posB, 10f - topW - bottomW));

		View view = null;
		if (widget!=null&&!widget.equals("")) {
			if (!widget.contains(".")) {
				if (widget.toLowerCase().startsWith("view")) {
					widget = "android.view." + widget;
				} else {
					widget = "android.widget." + widget;
				}
			}
			try {
				view = (View) Class.forName(widget)
						.getConstructor(Context.class, AttributeSet.class)
						.newInstance(context, attrs);
				setId(-1);
				int id = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/"+NS.nameSpace,
						"pid",-1);
				view.setId(id);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			view.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
			((ViewGroup) findViewById(R.id.content)).addView(view);
		}
		requestLayout();
		setBackgroundColor(android.R.color.transparent);
	}

	public LinearLayout.LayoutParams getLayoutParam(int areaP, float percent,
			Class clazz) {
		int width = 0;
		int height = 0;
		switch (areaP) {
		case posL:
		case posR:
			width = 0;
			height = LayoutParams.FILL_PARENT;
			break;
		case posT:
		case posB:
			width = LayoutParams.FILL_PARENT;
			height = 0;
			break;
		}
		LinearLayout.LayoutParams param = null;
		try {
			param = (LinearLayout.LayoutParams) clazz.getConstructor(int.class,
					int.class).newInstance(width, height);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		param.weight = percent;
		return param;
	}

	public LinearLayout.LayoutParams getLayoutParam(int areaP, float percent) {
		int width = 0;
		int height = 0;
		switch (areaP) {
		case posL:
		case posR:
			width = 0;
			height = LayoutParams.FILL_PARENT;
			break;
		case posT:
		case posB:
			width = LayoutParams.FILL_PARENT;
			height = 0;
			break;
		}
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(width,
				height);
		param.weight = percent;
		return param;
	}

	@Override
	public void addView(View child, int width, int height) {
		// TODO Auto-generated method stub
		super.addView(child, width, height);
	}

	@Override
	public void addView(View child, int index) {
		// TODO Auto-generated method stub
		super.addView(child, index);
	}

	@Override
	public void addView(View child) {
		// TODO Auto-generated method stub
		super.addView(child);
	}
	

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		if (((ViewGroup) findViewById(R.id.content)).getChildCount() == 0) {
			if (getChildCount() > 1) {
				
			}
		}
	}

	@Override
	public void addView(View child, int index,
			android.view.ViewGroup.LayoutParams params) {
		// TODO Auto-generated method stub
		super.addView(child, index, params);
	}

	@Override
	public void addView(View child, android.view.ViewGroup.LayoutParams params) {
		// TODO Auto-generated method stub
		if(getChildCount()==0){
			super.addView(child, params);
			return;
		}
		if (((RelativeLayout) findViewById(R.id.content)).getChildCount() == 0) {
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(params);
			((RelativeLayout) findViewById(R.id.content)).addView(child, layoutParams);
			return;
	}
		super.addView(child, params);
	}

	@Override
	protected void attachViewToParent(View child, int index,
			android.view.ViewGroup.LayoutParams params) {
		// TODO Auto-generated method stub
		super.attachViewToParent(child, index, params);
	}
	
}
